import json
from PyCmpltrtok.common import get_dir_name_ext, sep
import os


def jsonl2json(jsonl_path, limit=None):
    if limit:
        assert(isinstance(limit, int))
        suffix = f'.limit-{limit}.json'
    else:
        suffix = '.json'
    xdir, xname, xext = get_dir_name_ext(jsonl_path)
    json_path = os.path.join(xdir, xname + suffix)
    sep('Started: ' + xname)
    with open(jsonl_path, 'r', encoding='utf8') as fin, open(json_path, 'w', encoding='utf8') as fout:
        fout.write('[')
        xfirst = True
        cnt = -1
        while True:
            xline = fin.readline()
            if not xline:
                break
            cnt += 1
            if not cnt % 1000:
                print(cnt)
            if limit:
                if cnt > limit:
                    break
            if '\r\n' == xline[-2:]:
                xline = xline[:-2]
            elif '\n' == xline[-1:]:
                xline = xline[:-1]
            if xfirst:
                xfirst = False
            else:
                fout.write(',')
            fout.write(xline + '\n')
        fout.write(']\n')
    print(cnt)
    sep('Over: ' + xname)


if '__main__' == __name__:
    jsonl_paths = [
        # r'D:\_dell7590_root\local\LNP_datasets\med\CBLUE\CMeIE-V2\CMeIE-V2_train.jsonl',
        # r'D:\_dell7590_root\local\LNP_datasets\med\CBLUE\CMeIE-V2\CMeIE-V2_dev.jsonl',
        # r'D:\_dell7590_root\local\LNP_datasets\med\CBLUE\CMeIE-V2\CMeIE-V2_test.jsonl',
        # r'D:\_dell7590_root\local\LNP_datasets\med\CBLUE\CMeIE-V2\example_pred.jsonl',
        # r'D:\_dell7590_root\local\LNP_datasets\AdvertiseGen\dev.jsonl',
        # r'D:\_dell7590_root\local\LNP_datasets\AdvertiseGen\train.jsonl',
        r'D:\_dell7590_root\local\LNP_datasets\med\CBLUE\CMeIE-V2-002\CMeIE-V2_train.limit-20.jsonl',
    ]
    for jsonl_path in jsonl_paths:
        print(jsonl_path)
        jsonl2json(jsonl_path, limit=0)
    sep('All over')